package com.cnware.connection;

import java.io.File;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;

public class ConnectionManager {
	private static HashMap<String, ConnectionPool> poolMap = new HashMap<String, ConnectionPool>();
	
	private static ConnectionManager instance;
	
	private ConnectionManager(){}
	
	public static ConnectionManager getInstance(){
		if(instance == null)
			instance = new ConnectionManager();
		return instance;
	}
	
	public Connection getConncection(String dbFile){
		try {
			return getConnection(dbFile, true);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public Connection getConnection(String dbFile, boolean create) throws FileNotFoundException{
		File f = new File(dbFile);
		if(!f.exists() && !create){
			throw new FileNotFoundException(dbFile + " - DB File is not exist!");
		}
		ConnectionPool pool = new ConnectionPool(dbFile);
		poolMap.put(dbFile, pool);
		return pool.getConnection();
	}
	
	public static void main(String[] args) throws ClassNotFoundException {
		// load the sqlite-JDBC driver using the current class loader
	    Class.forName("org.sqlite.JDBC");
	    
	    Connection connection = null;
	    try
	    {
	      // create a database connection
	      connection = DriverManager.getConnection("jdbc:sqlite:sample.db");
	      Statement statement = connection.createStatement();
	      statement.setQueryTimeout(30);  // set timeout to 30 sec.
	      
//	      statement.executeUpdate("drop table if exists person");
	      statement.executeUpdate("create table if not exists person (id integer, name string)");
	      statement.executeUpdate("insert into person values(1, 'leo11111113')");
	      statement.executeUpdate("insert into person values(2, 'yui22222224')");
	      ResultSet rs = statement.executeQuery("select * from person");
	      
//	      new Thread(new test("111")).start();
//	      new Thread(new test("222")).start();
//	      new Thread(new test("333")).start();
//	      new Thread(new test("444")).start();
//	      new Thread(new test("1111")).start();
//	      new Thread(new test("2221")).start();
//	      new Thread(new test("3331")).start();
//	      new Thread(new test("4441")).start();
//	      new Thread(new test("1112")).start();
//	      new Thread(new test("2222")).start();
//	      new Thread(new test("3332")).start();
//	      new Thread(new test("4442")).start();
	      
	      while(rs.next())
	      {
	        // read the result set
	        System.out.println("name = " + rs.getString("name"));
	        System.out.println("id = " + rs.getInt("id"));
	      }
	    }
	    catch(SQLException e)
	    {
	      // if the error message is "out of memory", 
	      // it probably means no database file is found
	      System.err.println(e.getMessage());
	    }
	    finally
	    {
	      try
	      {
	        if(connection != null)
	          connection.close();
	      }
	      catch(SQLException e)
	      {
	        // connection close failed.
	        System.err.println(e);
	      }
	    }
	}
	
	
}


class test implements Runnable {
	
	private String aa = "";
	
	public test(String tag) {
		// TODO Auto-generated constructor stub
		this.aa = tag;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		System.out.println("start to Run " + aa + ".....");
		try {
			Class.forName("org.sqlite.JDBC");
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	    
	    Connection connection = null;
	    try
	    {
	      // create a database connection
	      connection = DriverManager.getConnection("jdbc:sqlite:sample.db");
	      Statement statement = connection.createStatement();
	      statement.setQueryTimeout(30);  // set timeout to 30 sec.
	      
	      statement.executeUpdate("drop table if exists person");
	      statement.executeUpdate("create table person (id integer, name string)");
	      statement.executeUpdate("insert into person values(3, 'leo3" + aa + "')");
	      statement.executeUpdate("insert into person values(4, 'yui4" + aa + "')");
	      ResultSet rs = statement.executeQuery("select * from person");
	      
	      while(rs.next())
	      {
	        // read the result set
	        System.out.println(">>>name = " + rs.getString("name"));
	        System.out.println(">>>id = " + rs.getInt("id"));
	      }
	    }
	    catch(SQLException e)
	    {
	      // if the error message is "out of memory", 
	      // it probably means no database file is found
	      System.err.println(e.getMessage());
	    }
	    finally
	    {
	      try
	      {
	        if(connection != null)
	          connection.close();
	      }
	      catch(SQLException e)
	      {
	        // connection close failed.
	        System.err.println(e);
	      }
	    }
	}
}
